package sky.wicket;

/**
 * Created with IntelliJ IDEA.
 * User: Gauss
 * Date: 06.10.13
 * Time: 19:28
 * To change this template use File | Settings | File Templates.
 */
public class PascalTriangle {

    public static int calcTriangle(int column, int row) throws NumberNotFoundException {
        return firstMethod(column, row);
    }

    public static int firstMethod(int column, int row) throws NumberNotFoundException {
        if (column>row || row<0 || column<0) throw new NumberNotFoundException();
        if (column==0) return 1;
        if (column==row) return 1;
        return calcTriangle(column, row-1)+calcTriangle(column-1, row-1);
    }

    public static int secondMethod(int column, int row) throws NumberNotFoundException {
        if (column>row || row<0 || column<0) throw new NumberNotFoundException();
        return factorial(row)/(factorial(column)*factorial(row - column));
    }

    public static int factorial(int n) {
        if (n<=1) return 1;
        return n*factorial(n-1);
    }

    public static class NumberNotFoundException extends Exception {

        public NumberNotFoundException() {
            super("Выход за пределы треугольника");
        }
    }
}
